<template>
  <div class="left-flex">
    <div class="title flex-view">
      <h3>订单明细</h3>
    </div>
    <div class="cart-list-view">
      <div class="list-th flex-view">
        <span class="line-1">商品名称</span>
        <span class="line-2">价格</span>
        <span class="line-5">数量</span>
        <!-- <span class="line-6">操作</span> -->
      </div>
      <div class="list">
        <div class="items flex-view">
          <div class="book flex-view">
            <img :src="pageData.cover" />
            <h2>{{ pageData.title }}</h2>
          </div>
          <div class="pay">¥{{ pageData.price }}</div>
          <el-input-number
            style="width: 100px"
            v-model="pageData.amount"
            :min="1"
            :max="99"
            @change="onCountChange(pageData)"
          />
          <!-- <img :src="DeleteIcon" class="delete" /> -->
        </div>
      </div>
    </div>
    <div class="title flex-view">
      <h3>备注</h3>
    </div>
    <textarea
      v-model="pageData.remark"
      placeholder="输入备注信息,100字以内"
      class="remark"
    >
    </textarea>
  </div>
  <div class="right-flex">
    <div class="title flex-view">
      <h3>收货地址</h3>
    </div>
    <div class="address-view">
      <div class="address" v-if="pageData.receiverAddress">
        <div class="info" style="">
          <p>
            收件人：&nbsp;&nbsp;&nbsp;&nbsp;
            <span class="name">{{ pageData.receiverName }} </span>
          </p>
          <p>
            电&nbsp;&nbsp;&nbsp;话： &nbsp;&nbsp;&nbsp;&nbsp;<span class="tel"
              >{{ pageData.receiverPhone }}
            </span>
          </p>
          <p>
            省&nbsp;&nbsp;&nbsp;份： &nbsp;&nbsp;&nbsp;&nbsp;<span class="tel"
              >{{ pageData.provinceName }}
            </span>
          </p>
          <p>
            详细地址：
            <span class="address"> {{ pageData.receiverAddress }}</span>
          </p>
        </div>
      </div>
      <div class="info" v-else>
        <span>目前暂无默认地址，请</span>
        <span class="info-blue" @click="handleAdd">新建地址</span>
      </div>
    </div>
    <div class="title flex-view">
      <h3>结算</h3>
      <span class="click-txt">价格</span>
    </div>
    <div class="price-view">
      <div class="price-item flex-view">
        <div class="item-name">商品总价</div>
        <div class="price-txt">¥{{ pageData.totalSum }}</div>
      </div>
      <div class="price-item flex-view">
        <div class="item-name">商品优惠</div>
        <div class="price-txt">¥0</div>
      </div>
      <!-- <div class="price-item flex-view">
        <div class="item-name">商品折扣</div>
        <div class="price-txt">¥0</div>
      </div> -->
      <div class="total-price-view flex-view">
        <span>合计</span>
        <div class="price">
          <span class="font-big">¥{{ pageData.totalSum }}</span>
        </div>
      </div>
      <!-- <div class="btns-view">
        <button class="btn buy" @click="handleBack">返回</button>
        <button class="btn pay jiesuan" @click="handleBuy(pageData)">
          结算
        </button>
      </div> -->
    </div>
    <el-dialog
      style="width: 500px; border-radius: 10px"
      v-model="addressDialog.isVisible"
      :title="addressDialog.title"
      align-center
    >
      <el-form label-width="100px" label-position="left">
        <el-form-item label="姓名">
          <el-input v-model="pageData.receiverName" />
        </el-form-item>
        <el-form-item label="电话号码">
          <el-input v-model="pageData.receiverPhone" />
        </el-form-item>
        <el-form-item label="所在省份">
          <el-select
            v-model="optionValue"
            placeholder="请选择省份"
            size="large"
            style="height: 30px"
          >
            <el-option
              v-for="item in provineOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="详细地址">
          <el-input v-model="pageData.receiverAddress" />
        </el-form-item>

        <el-form-item label="默认地址">
          <span>否&nbsp;</span>
          <el-switch
            v-model="pageData.isDefault"
            active-value="1"
            inactive-value="0"
          />
          <span>&nbsp;是</span>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <!-- <el-button @click="centerDialogVisible = false">Cancel</el-button> -->
          <el-button type="primary" @click="save(pageData)"> 保存 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { defineProps, reactive, ref } from "vue";
import { provincePage } from "@/apis/province";
import { addressAdd } from "@/apis/user/address";

const props = defineProps({
  pageData: {
    type: Object,
    required: true,
  },
});

// 商品数量发生改变
const onCountChange = (value) => {
  // console.log(value.amount);
  value.totalSum = value.price * value.amount;
};

/**
 * 购买详情对话框
 */
const addressDialog = reactive({
  isVisible: false,
  title: "用户地址",
});
// 控制对话框是否显示
const handleAdd = () => {
  addressDialog.isVisible = true;
  // console.log(scope)
  getProvincePage();
};

// 地址表单
const addressForm = reactive({});

// 添加地址函数
const newAddress = reactive({});
const getAddressAdd = (value) => {
  newAddress.isDefault = value.isDefault;
  newAddress.provinceId = optionValue.value;
  newAddress.description = value.receiverAddress;
  newAddress.name = value.receiverName;
  newAddress.mobile = value.receiverPhone;
  // console.log(newAddress);

  addressAdd(newAddress).then((result) => {
    // console.log(result);
    if (result != undefined) {
      // 关闭弹窗
      addressDialog.isVisible = false;
    }
  });
};

// 触发地址保存
const save = async (value) => {
  // console.log(value);
  // 加上省份ID
  value.provinceId = optionValue.value;

  const result = await getAddressAdd(value);
  // console.log(result);
};

// 省份

// 获取省份

const provincePagination = reactive({
  page: 1,
  size: 100,
});
const getProvincePage = () => {
  provincePage({}, provincePagination.page, provincePagination.size).then(
    (result) => {
      provineOptions.value = result.data.records;
      // console.log(provineOptions.value);
    }
  );
};

// 多选框默认值
const optionValue = ref("");
// 多选框取值
const provineOptions = ref([]);
</script>

<style scope lang="less">
.flex-view {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
}

.cart-page {
  width: 1024px;
  min-height: 50vh;
  margin: 100px auto;
}

.left-flex {
  -webkit-box-flex: 17;
  -ms-flex: 17;
  flex: 17;
  padding-right: 20px;
}

.title {
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;

  h3 {
    color: #152844;
    font-weight: 600;
    font-size: 18px;
    height: 26px;
    line-height: 26px;
    margin: 0;
  }
}

.cart-list-view {
  margin: 4px 0 40px;
  padding-left: 5px;

  .list-th {
    height: 42px;
    line-height: 42px;
    border-bottom: 1px solid #cedce4;
    color: #152844;
    font-size: 14px;
    // margin-left: 150px;

    .line-1 {
      -webkit-box-flex: 1;
      -ms-flex: 1;
      flex: 1;
      margin-right: 20px;
    }

    .line-2,
    .pc-style .cart-list-view .list-th .line-3,
    .pc-style .cart-list-view .list-th .line-4 {
      width: 65px;
      margin-right: 40px;
    }

    .line-5 {
      width: 80px;
      // margin-left: 15px;
    }

    .line-6 {
      width: 28px;
    }
  }
}

.items {
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  margin-top: 20px;

  .book {
    -webkit-box-flex: 1;
    -ms-flex: 1;
    flex: 1;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    margin-right: 20px;
    cursor: pointer;

    img {
      width: 48px;
      margin-right: 16px;
      border-radius: 4px;
    }

    h2 {
      -webkit-box-flex: 1;
      -ms-flex: 1;
      flex: 1;
      font-size: 14px;
      line-height: 22px;
      color: #152844;
    }
  }

  .type {
    width: 65px;
    margin-right: 20px;
    color: #152844;
    font-size: 14px;
  }

  .pay {
    color: #ff8a00;
    font-weight: 600;
    font-size: 16px;
    width: 65px;
    margin-right: 20px;
  }

  .num-box {
    width: 80px;
    margin-right: 43px;
    border-radius: 4px;
    border: 1px solid #cedce4;
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    height: 32px;
    padding: 0 4px;
  }

  .delete {
    margin-left: 36px;
    width: 24px;
    cursor: pointer;
  }
}

.mb-24 {
  margin-bottom: 24px;
}

.show-txt {
  color: #ff8a00;
  font-size: 14px;
}

.remark {
  width: 100%;
  background: #f6f9fb;
  border: 0;
  border-radius: 4px;
  padding: 6px 12px;
  //color: #152844;
  margin-top: 16px;
  resize: none;
  height: 56px;
  line-height: 22px;
}

.right-flex {
  -webkit-box-flex: 8;
  -ms-flex: 8;
  flex: 8;
  padding-left: 5px;
  border-left: 1px solid #cedce4;
}

.click-txt {
  color: #4684e2;
  font-size: 14px;
  cursor: pointer;
}

.address-view {
  margin: 12px 0 24px;

  .info {
    color: #909090;
    font-size: 14px;

    .info-blue {
      cursor: pointer;
      color: #4684e2;
    }
    p {
      padding: 10px;
      // margin: 10px;
    }
  }

  .name {
    color: #152844;
    font-weight: 600;
  }

  .tel {
    color: #152844;
    font-weight: 600;
  }

  .address {
    color: #152844;
    font-weight: 600;
  }
}

.price-view {
  overflow: hidden;
  margin-top: 16px;

  .price-item {
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    margin-bottom: 8px;
    font-size: 14px;

    .item-name {
      color: #152844;
    }

    .price-txt {
      font-weight: 500;
      color: #ff8a00;
    }
  }

  .total-price-view {
    margin-top: 12px;
    border-top: 1px solid #cedce4;
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;
    -webkit-box-align: start;
    -ms-flex-align: start;
    align-items: flex-start;
    padding-top: 10px;
    color: #152844;
    font-weight: 500;

    .price {
      color: #ff8a00;
      font-size: 16px;
      height: 36px;
      line-height: 36px;
    }
  }

  .btns-view {
    margin-top: 24px;
    text-align: right;

    .buy {
      background: #fff;
      color: #4684e2;
    }

    .jiesuan {
      cursor: pointer;
      background: #4684e2;
      color: #fff;
    }

    .btn {
      cursor: pointer;
      width: 96px;
      height: 36px;
      line-height: 33px;
      margin-left: 16px;
      text-align: center;
      border-radius: 32px;
      border: 1px solid #4684e2;
      font-size: 16px;
      outline: none;
    }
  }
}
</style>
